package com.example.demo.service.impl;

import com.example.demo.dao.DishesDao;
import com.example.demo.dao.UserDao;
import com.example.demo.model.Dishes;
import com.example.demo.model.User;
import com.example.demo.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private  UserDao userDao;
    @Autowired
    private  DishesDao dishesDao;

    @Override
    public void register(User user) {
        user.setPassword(encryptPassword(user.getPassword()));
        userDao.insert(user);
    }

    @Override
    public String login(String name, String password) {
        User user = userDao.findByName(name);
        if (user == null || !encryptPassword(password).equals(user.getPassword())) {
            throw new IllegalArgumentException("Invalid name or password");
        }
        // 在这里生成并返回用户的认证 token
        return generateToken(user.getUserId());
    }


    @Override
    public String encryptPassword(String password) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] encodedHash = digest.digest(password.getBytes());
            return Base64.getEncoder().encodeToString(encodedHash);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Error while hashing password", e);
        }
    }

    @Override
    public String generateToken(Long userId) {
        // 在这里生成用户的认证 token
        // 可以使用 JWT 或其他认证方案来生成 token
        return "";
    }

    @Override
    public void addDishToUser(Long userId, Long dishId) {
        User user = userDao.selectByPrimaryKey(userId);
        Dishes dish = dishesDao.selectByPrimaryKey(dishId);

        if (user != null && dish != null) {
            user.getDishes().add(dish);
        }
    }

    @Override
    public void shareDishWithUser(Long userId, Long dishId, Long otherUserId) {
        User user = userDao.selectByPrimaryKey(userId);
        Dishes dish = dishesDao.selectByPrimaryKey(dishId);
        User otherUser = userDao.selectByPrimaryKey(otherUserId);

        if (user != null && dish != null && otherUser != null) {
            user.shareDishWithUser(dish, otherUser);
//            userDao.insert(user);
//            userDao.insert(otherUser);
        }
    }

    @Override
    public void addViewDishToUser(String name, Long dishId) {
        User user = userDao.findByName(name);
        Dishes dish = dishesDao.selectByPrimaryKey(dishId);
        System.out.println(dish);

        if (user != null && dish != null) {
            user.addViewedDish(dish);
            dish.increaseViews();
        }
    }

    @Override
    public User findByName(String username) {
        User user = userDao.findByName(username);
        return user;
    }

    @Override
    public int insert(User newUser) {
        return userDao.insert(newUser);
    }

    @Override
    public int update(User existingUser) {
        return userDao.updateByPrimaryKey(existingUser);
    }

    @Override
    public int updateByPrimaryKey(User user) {
        return userDao.updateByPrimaryKey(user);
    }

}
